package com.topfinance.payment.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.topfinance.frame.dao.HibernateEntityDao;
import com.topfinance.frame.exception.AppException;
import com.topfinance.frame.util.StringUtil;
import com.topfinance.payment.ebo.TC2MsgListEbo;
import com.topfinance.payment.service.ITC2MsgListMgr;

/**
 * 该类自动实现了dao的一般方法，如add, remove, update
 * 在service中出现的大部分情况下是在向后台提交数据前的处理(如校验)及获取后台数据后的处理等
 * @author
 *
 */

public class TC2MsgListMgrImpl extends HibernateEntityDao<TC2MsgListEbo> implements ITC2MsgListMgr {

	private static final Log log = LogFactory.getLog(TC2MsgListMgrImpl.class);
	
	//获得传输协议	
	public TC2MsgListEbo getTC2MsgList(Integer oid) throws AppException{
		return (TC2MsgListEbo)get(oid);
	}

	public void deleteTC2MsgList(Integer oid) throws AppException{
		removeById(TC2MsgListEbo.class,oid);
	}

	//获得TC2MsgListEbo对象
	//根据msgcode,如果msgcode对应的对象不唯一，异常
	public TC2MsgListEbo getMsgListEbo(String msgcode) throws AppException{
		if(StringUtil.isNullStr(msgcode))
			return null;
		List<TC2MsgListEbo> msgs = findBy("outMsgCode", msgcode);
		if(msgs.size()==1)
			return msgs.get(0);
		else if(msgs.size()>1)
			throw new AppException("[对应的TC2MsgListEbo重复]" + msgcode);
		else
			return null;
	}
	
	//获得报文列表
	public List<TC2MsgListEbo> getAllMsgs(String systype,String app) throws AppException{
		String hql = " from TC2MsgListEbo l " ;
		String where = "";
		if (systype != null)
			where += " and l.sysType ='" + systype + "'";
		if (app != null) 
			where += " and l.appModule = '" + app + "'";
		//只选取当前允许的报文
		where += " and l.permited = 'Y'";
		List<Object> paramList = new ArrayList<Object>();
		if (isNotBlank(where)) {
			where = " where " + where.substring(4);
		}
		hql = hql + where;
		return find(hql,paramList.toArray());		
	}
	
	//获得TC2MsgListEbo
	//根据module=CNAPS2，app=HVPS，BEPS，SAPS和CCMS
	//model=1 模式一，model=2 模式二，model=4 模式三
	public List<TC2MsgListEbo> getAllMsgByPOModel(String systype,String app,int model) throws AppException{
		//select l.out_msg_code,l.out_msg_name from T_C2_MSG_LIST l,t_c2_msg_inout io
		//where l.permited = 'Y' and l.app_module = 'HVPS' and l.out_msg_code = io.out_msg_code and io.po = 1
		
		String hql = " select l from TC2MsgListEbo l,TC2MsgInoutEbo io " ;
		String where = "";
		if (systype != null)
			where += " and l.sysType ='" + systype + "'";
		if (app != null) 
			where += " and l.appModule = '" + app + "'";
		//where += " and l.permited = 'Y'";
		//只选取当前允许的报文
		where += " and l.permited = 'Y'";
		where += " and l.outMsgCode = io.outMsgCode";
		if(model>-1)
			where += " and io.po = " + model;
		List<Object> paramList = new ArrayList<Object>();
		if (isNotBlank(where)) {
			where = " where " + where.substring(4);
		}
		hql = hql + where;
		return find(hql,paramList.toArray());		
	}
	
}
